package com.zhu.storesmanager.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.preference.PreferenceManager;

public class PreferenceStore 
{
    private Context activity ; 
	
	private static PreferenceStore instance ;
	public static PreferenceStore getInstance(Context context)  {
		if (instance == null) {
			instance = new PreferenceStore(context);
		}
		return instance ;
	}
	
    private PreferenceStore(Context context) {
    	activity = context;
    }

    public static long DAY_MS = 86400 * 1000;

    public boolean needToUpdate(String category, long period) 
    {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
        long now = System.currentTimeMillis();
        String key = "lastupdate_" + category;
        long lastTime = prefs.getLong(key, -1);
        if (now - lastTime > period) 
        {
            SharedPreferences.Editor editor = prefs.edit();
            editor.putLong(key, now);
            editor.commit();
            return true;
        } 
        else 
        {
            return false;
        }
    }

    public boolean isUpdate() {
    	boolean isUpdate = false;
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
        int versionCodeOld = prefs.getInt("versionCode", 0);
        String versionNameOld = prefs.getString("versionName", "0.0.0.0");

        int versionCodeNew = 0;
        String versionNameNew = "";
        try {
        	PackageManager pm = activity.getPackageManager();  
        	PackageInfo pi = pm.getPackageInfo(activity.getPackageName(), 0);
        	versionCodeNew = pi.versionCode;
            versionNameNew = pi.versionName;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

        if (versionNameOld == null || versionNameOld.length() <= 0 || 
        		versionNameNew == null || versionNameNew.length() <= 0 ||
        	versionCodeOld != versionCodeNew || !versionNameOld.equals(versionNameNew)){
        	isUpdate = true;
        }
        if (isUpdate) {
	        SharedPreferences.Editor editor = prefs.edit();
	        editor.putInt("versionCode", versionCodeNew);
	        editor.putString("versionName", versionNameNew);
	        editor.commit();
        }
        return isUpdate;
    }
    
    public void putString(String prop, String value) 
    {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(prop, value);
        editor.commit();
    }

    public void putInt(String prop, int value) 
    {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt(prop, value);
        editor.commit();
    }

    public void putLong(String prop, long value) 
    {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putLong(prop, value);
        editor.commit();
    }

    public String getString(String prop, String defValue) 
    {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
        return prefs.getString(prop, defValue);
    }

    public void putBoolean(String prop, boolean value) 
    {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean(prop, value);
        editor.commit();
    }

    public boolean getBoolean(String prop, boolean defValue) 
    {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
        return prefs.getBoolean(prop, defValue);
    }

    public int getInt(String prop, int defValue) 
    {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
        return prefs.getInt(prop, defValue);
    }

    public long getLong(String prop, long defValue) 
    {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
        return prefs.getLong(prop, defValue);
    }
}